/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.ui;

import gov.fema.hazus.bdc.client.Constants;
import gov.fema.hazus.bdc.client.UIController;
import gov.fema.hazus.bdc.client.common.entities.AbstractClickListener;
import gov.fema.hazus.bdc.client.common.ui.Message;
import gov.fema.hazus.bdc.client.controller.Controller;
import gov.fema.hazus.bdc.client.login.Login;
import gov.fema.hazus.bdc.shared.entity.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class TopNavigation extends Composite {

    private class LogoutClickListener extends AbstractClickListener
    {
        @Override
        public void handleClick(Widget sender) {
            UIController.get().logout();
        }   
    }
    
    private class MapButtonClickListener extends AbstractClickListener
    {
        @Override
        public void handleClick(Widget sender) {
        	UIController.get().showBuildingMap();
        }   
    } 
    
    private class ListButtonClickListener extends AbstractClickListener
    {
        @Override
        public void handleClick(Widget sender) {
        	UIController.get().displayListView();   
        }   
    }    
    
	private static TopNavigation singleton;
	public static TopNavigation get()
	{
        if( singleton == null )
        {
            singleton = new TopNavigation();
        }
        return singleton;
	}
    
    
    // widgets
    private HorizontalPanel main;
    private HorizontalPanel topRight;
    private HorizontalPanel topLeft;
    private Constants constants;
    private Label loggedInUser;
    
    public TopNavigation()
    {
      
        singleton = this;
        
        // constants
        constants = (Constants)GWT.create(Constants.class);
        
        // setup 
        main = new HorizontalPanel();
        main.setStyleName("appnav");
        topRight = new HorizontalPanel();

        
        // get user from Login component
        User currentUser = Login.get().getLoginUser();
        // if the Login user is empty, check the cookie
        if( currentUser.getUsername() == null )
        {
            // during logout, the User is reset in the Login class
            // so we don't have a username
            String usernameFromCookie = this.getUsernameFromCookie();
            currentUser.setUsername(usernameFromCookie);
        }
        
        GWT.log("user=" + currentUser, null);
        loggedInUser = new Label(currentUser.getUsername());
        loggedInUser.addStyleName("user");
        
        // other links
        
        Label settingsLabel = new Label( constants.topnavSettings() );
        settingsLabel.addStyleName("item");
        settingsLabel.addClickListener(new ClickListener() {
            public void onClick(Widget arg0) {
            	LeftNavigation.get().highlightLeftNav("NONE");
                Controller.get().mainShowSettings();
            }
        });
        
        Label help = new Label( constants.topnavHelp() );
        help.addStyleName("item");
        
        Label signOut = new Label( constants.topnavSignOut() );
        signOut.addClickListener(new ClickListener() {
            public void onClick(Widget arg0) {
                Login.get().logout();
            }
        });     
        signOut.addStyleName("item");
        
        topRight.add(loggedInUser);
        topRight.add(new HTML("|"));
        topRight.add(settingsLabel);
        topRight.add(new HTML("|"));
        
        // TODO: Add Help
        //topRight.add(help);
        //topRight.add(new HTML("|"));
        
        topRight.add(signOut);
        topRight.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        
        //  dummy
        String links = "<a target=\"_blank\" href=\"http://www.fema.gov/plan/prevent/hazus/\">FEMA: HAZUS</a>";
        links += "&nbsp;&nbsp;";
        links += "<a target=\"_blank\" href=\"http://www.hazus.org/\">HAZUS.org</a>";
        links += "&nbsp;&nbsp;";
        links += "<a target=\"_blank\" href=\"http://www.usehazus.com/\">USEHAZUS</a>";

        HTML left =  new HTML(links);
        main.add(left);
        main.setCellWidth(left, "300");
        main.setCellHorizontalAlignment(left, HasHorizontalAlignment.ALIGN_LEFT);
        
        // middle
        Message message = Message.get();
        main.add(message);
        main.setCellWidth(message, "400px");
        main.setCellHorizontalAlignment(message, HasHorizontalAlignment.ALIGN_CENTER);
        
        main.add(topRight);
        main.setCellHeight(topRight, "20");
        main.setCellHorizontalAlignment(topRight, HasHorizontalAlignment.ALIGN_RIGHT);
        
        initWidget(main);
        
        //Window.addWindowResizeListener(this);

    }
    
    private String getUsernameFromCookie() {
    	return Login.get().getUserNameFromCookie();
    }    
    
    public void updateUserName()
    {
        String userName = Login.get().getUserNameFromCookie();
        loggedInUser.setText( userName );
    }

	public void onWindowResized(int width, int height) {
		GWT.log("topnav: resize", null);
		String topnavWidth = Integer.toString(width) + "px";
		main.setWidth(topnavWidth);
	}

}
